package com.tianyang.yukuaifu.wxapi;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tianyang.yukuaifu.BuildConfig;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.jump.shareutil.LoginUtil;
import com.tianyang.yukuaifu.jump.shareutil.ShareUtil;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
	private static final String TAG = WXEntryActivity.class.getSimpleName();

	private IWXAPI api;

	private int mType;

	private boolean isNew;

	private static final String TYPE = "share_activity_type";

	public static Intent newInstance(Context context, int type) {
		Intent intent = new Intent(context, WXEntryActivity.class);
		if (context instanceof Application) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		}
		intent.putExtra(TYPE, type);
		return intent;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		api = WXAPIFactory.createWXAPI(this, BuildConfig.WX_APP_ID, false);
		try {
			Intent intent = getIntent();
			api.handleIntent(intent, this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		isNew = true;

		// init data
		mType = getIntent().getIntExtra(TYPE, 0);
		if (mType == ShareUtil.TYPE) {
			// 分享
			ShareUtil.action(this);
		} else if (mType == LoginUtil.TYPE) {
			// 登录
			LoginUtil.action(this);
		} else {
			// handle 微信回调
			LoginUtil.handleResult(-1, -1, getIntent());
			ShareUtil.handleResult(getIntent());
			finish();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isNew) {
			isNew = false;
		} else {
			finish();
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		setIntent(intent);
		api.handleIntent(intent, this);

		if (mType == LoginUtil.TYPE) {
			LoginUtil.handleResult(0, 0, intent);
		} else if (mType == ShareUtil.TYPE) {
			ShareUtil.handleResult(intent);
		}
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 处理回调
		if (mType == LoginUtil.TYPE) {
			LoginUtil.handleResult(requestCode, resultCode, data);
		} else if (mType == ShareUtil.TYPE) {
			ShareUtil.handleResult(data);
		}
		finish();
	}

	@Override
	public void onReq(BaseReq baseReq) {
		Log.d(TAG, "onReq: " + baseReq.getType());
		switch (baseReq.getType()) {
			case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
				break;
			case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
				goToShowMsg((ShowMessageFromWX.Req) baseReq);
				break;
			default:
				break;
		}

		finish();
	}

	private void goToShowMsg(ShowMessageFromWX.Req baseReq) {
		Log.d(TAG, "goToShowMsg:"+baseReq.message);
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e(TAG, "onResp: " + resp.getType());
		if (resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
			WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) resp;
			String extraData = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
			//extraData: 0000支付成功，9999支付失败
			Log.e(TAG, "extraData：" + extraData);
			Intent intent = new Intent();
			intent.setAction(CommonConfig.WX_PAY_RESULT_ACTION);
			if (TextUtils.equals("0000", extraData)) {
				intent.putExtra(CommonConfig.WX_PAY_RESULT, "0");
			} else {
				intent.putExtra(CommonConfig.WX_PAY_RESULT, "-1");
			}
			sendBroadcast(intent);
		}
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
}
	
